package cc.cc4414.spring.common.observer;

import cc.cc4414.spring.common.result.CommonResultEnum;
import cc.cc4414.spring.common.result.ResultException;
import lombok.experimental.UtilityClass;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 消息工具
 *
 * @author cc 2019年10月1日
 */
@UtilityClass
public class MessageUtils {
    /**
     * 发送消息
     *
     * @param channel 消息频道或者被观察者
     * @param object  消息体，通常为{@link MessagePayload}
     */
    public void send(MessageChannel channel, Object object) {
        if (!channel.send(MessageBuilder.withPayload(object).build())) {
            throw new ResultException(CommonResultEnum.MESSAGE_SEND_FAILED);
        }
    }
}
